package com.qf.controller;

import com.qf.pojo.TbAddress;
import com.qf.pojo.TbUser;
import com.qf.service.AddressService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/15 19:20
 */
@WebServlet("/address")
public class AddressServlet extends BaseServlet{
    public String  show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        TbUser user = (TbUser) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }

        String aname = request.getParameter("aname");
        String aphone = request.getParameter("aphone");
        String adetail = request.getParameter("adetail");
        System.out.println("adetail = " + adetail);
        System.out.println("aname = " + aname);
        if(StringUtil.isEmpty(aname)){
            request.setAttribute("msg", "收件人不可为空");
            return "self_info.jsp";
        }
        if(StringUtil.isEmpty(aphone)){
            request.setAttribute("msg", "收件人不可为空");
            return "self_info.jsp";
        }
        if(StringUtil.isEmpty(adetail)){
            request.setAttribute("msg", "收件人不可为空");
            return "self_info.jsp";
        }
        AddressService addressService=new AddressServiceImpl();
        TbAddress tbAddress=new TbAddress();
        tbAddress.setName(aname);
        tbAddress.setPhone(aphone);
        tbAddress.setDetail(adetail);
        tbAddress.setUid(user.getId());
        tbAddress.setLevel(0);
        addressService.add(tbAddress);


        return "redirect:/address?method=getAddress";
    }
    public String  getAddress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        TbUser user = (TbUser) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        try {
            AddressService addressService=new AddressServiceImpl();
            List<TbAddress> tbAddress=addressService.getAddress(user.getId());
            request.setAttribute("addList", tbAddress);
        } catch (Exception e) {
            request.setAttribute("msg", "查询地址信息失败"+e.getMessage());
        }

        return "/self_info.jsp";
    }
}
